using System;
using System.Data;
using System.Text;
using System.Configuration;

using External.HtmlCleaner;

namespace Grr.Model
{
    public static class HtmlCleaner
    {
        private static readonly string sammich_open_tag = "<div>";
        private static readonly string sammich_close_tag = "</div>";

        public static string CleanHtml (string html)
        {
            html = String.Concat(sammich_open_tag, html, sammich_close_tag);

            StringBuilder builder = new StringBuilder ();
            HtmlReader reader = new HtmlReader(html);
            HtmlWriter writer = new HtmlWriter (builder);
            
            writer.FilterOutput = true;

            reader.Read();
            writer.WriteNode(reader, true);

            int open_len = sammich_open_tag.Length;
            int close_ind;
            
            html = builder.ToString ();

            if (html.StartsWith ("<html>"))
                open_len += "<html>".Length;

            close_ind = html.LastIndexOf("</div>");

            html = html.Substring (open_len, close_ind - open_len);
            return html;
        }
    }
}